using DevExpress.XtraEditors;
using System;
using System.Data;
using System.Windows.Forms;

namespace MesProjectWinform.用户控件
{
    public partial class PaginationDemoForm : XtraForm
    {
        // 模拟数据源
        private DataTable dataSource;
        
        // 分页后的数据
        private DataTable currentPageData;

        public PaginationDemoForm()
        {
            InitializeComponent();
            
            // 初始化模拟数据
            InitializeData();
            
            // 初始化分页控件
            InitializePagination();
        }

        private void InitializeComponent()
        {
            this.gridControl1 = new DevExpress.XtraGrid.GridControl();
            this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
            this.paginationControl1 = new MesProjectWinform.用户控件.PaginationControl();
            this.panelControl1 = new DevExpress.XtraEditors.PanelControl();
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).BeginInit();
            this.panelControl1.SuspendLayout();
            this.SuspendLayout();
            // 
            // gridControl1
            // 
            this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridControl1.Location = new System.Drawing.Point(0, 0);
            this.gridControl1.MainView = this.gridView1;
            this.gridControl1.Name = "gridControl1";
            this.gridControl1.Size = new System.Drawing.Size(800, 405);
            this.gridControl1.TabIndex = 0;
            this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
            this.gridView1});
            // 
            // gridView1
            // 
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsView.ShowGroupPanel = false;
            // 
            // paginationControl1
            // 
            this.paginationControl1.Appearance.BackColor = System.Drawing.Color.White;
            this.paginationControl1.Appearance.Options.UseBackColor = true;
            this.paginationControl1.CurrentPage = 1;
            this.paginationControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.paginationControl1.Location = new System.Drawing.Point(0, 0);
            this.paginationControl1.Name = "paginationControl1";
            this.paginationControl1.PageSize = 10;
            this.paginationControl1.Size = new System.Drawing.Size(800, 45);
            this.paginationControl1.TabIndex = 1;
            this.paginationControl1.TotalRecords = 0;
            // 
            // panelControl1
            // 
            this.panelControl1.Controls.Add(this.paginationControl1);
            this.panelControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelControl1.Location = new System.Drawing.Point(0, 405);
            this.panelControl1.Name = "panelControl1";
            this.panelControl1.Size = new System.Drawing.Size(800, 45);
            this.panelControl1.TabIndex = 2;
            // 
            // PaginationDemoForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.gridControl1);
            this.Controls.Add(this.panelControl1);
            this.Name = "PaginationDemoForm";
            this.Text = "分页控件演示";
            this.Load += new System.EventHandler(this.PaginationDemoForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).EndInit();
            this.panelControl1.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        private DevExpress.XtraGrid.GridControl gridControl1;
        private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
        private PaginationControl paginationControl1;
        private DevExpress.XtraEditors.PanelControl panelControl1;

        private void PaginationDemoForm_Load(object sender, EventArgs e)
        {
            // 初始加载第一页数据
            LoadPageData(1, paginationControl1.PageSize);
        }

        private void InitializeData()
        {
            // 创建模拟数据表
            dataSource = new DataTable();
            dataSource.Columns.Add("ID", typeof(int));
            dataSource.Columns.Add("名称", typeof(string));
            dataSource.Columns.Add("描述", typeof(string));
            dataSource.Columns.Add("创建时间", typeof(DateTime));
            
            // 添加100条模拟数据
            Random random = new Random();
            for (int i = 1; i <= 100; i++)
            {
                DataRow row = dataSource.NewRow();
                row["ID"] = i;
                row["名称"] = "项目 " + i;
                row["描述"] = "这是项目" + i + "的描述信息，包含一些随机内容 " + random.Next(1000, 9999);
                row["创建时间"] = DateTime.Now.AddDays(-random.Next(0, 365));
                dataSource.Rows.Add(row);
            }
        }

        private void InitializePagination()
        {
            // 设置总记录数
            paginationControl1.TotalRecords = dataSource.Rows.Count;
            
            // 设置每页显示记录数选项
            paginationControl1.PageSizeOptions = new System.Collections.Generic.List<int> { 5, 10, 20, 50, 100 };
            
            // 设置默认每页显示记录数
            paginationControl1.PageSize = 10;
            
            // 绑定页面改变事件
            paginationControl1.PageChanged += PaginationControl1_PageChanged;
            
            // 绑定每页记录数改变事件
            paginationControl1.PageSizeChanged += PaginationControl1_PageSizeChanged;
        }

        private void PaginationControl1_PageChanged(object sender, PageChangedEventArgs e)
        {
            // 加载指定页的数据
            LoadPageData(e.CurrentPage, e.PageSize);
        }

        private void PaginationControl1_PageSizeChanged(object sender, PageSizeChangedEventArgs e)
        {
            // 每页记录数改变时，重新加载数据
            LoadPageData(e.CurrentPage, e.PageSize);
        }

        private void LoadPageData(int pageIndex, int pageSize)
        {
            try
            {
                // 计算起始行索引和结束行索引
                int startIndex = (pageIndex - 1) * pageSize;
                int endIndex = Math.Min(startIndex + pageSize - 1, dataSource.Rows.Count - 1);
                
                // 创建当前页数据表
                currentPageData = dataSource.Clone();
                
                // 复制指定范围的数据行
                if (startIndex <= endIndex)
                {
                    for (int i = startIndex; i <= endIndex; i++)
                    {
                        currentPageData.ImportRow(dataSource.Rows[i]);
                    }
                }
                
                // 绑定数据到网格控件
                gridControl1.DataSource = currentPageData;
                
                // 刷新网格视图
                gridView1.BestFitColumns();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("加载数据时发生错误：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 